package cn.appsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appsys.biz.AppCategoryBiz;
import cn.appsys.biz.AppInfoBiz;
import cn.appsys.biz.AppVersionBiz;
import cn.appsys.biz.DataDictionaryBiz;
import cn.appsys.biz.DevUserBiz;
import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DevUser;

@Controller
public class DeveloperController {

	@Autowired
	private AppInfoBiz appInfoBiz;
	
	@Autowired
	private DevUserBiz devUserBiz;
	
	@Autowired
	private AppCategoryBiz appCategoryBiz;
	
	@Autowired
	private AppVersionBiz appVersionBiz;
	@Autowired
	private DataDictionaryBiz dataDictionaryBiz;
	
	@RequestMapping("/devLogin")
	public String devLogin(){
		return "devLogin";
	}
	
	@RequestMapping(value="reDevLogin",method=RequestMethod.POST)
	public String reDevLogin(Model model,String name,String password,HttpSession session){
		DevUser user = devUserBiz.checkDevUser(name, password);
		if(user != null){
			session.setAttribute("user", user);
			session.removeAttribute("admin");
			return "advocate";
		}
		model.addAttribute("checkInfo", "用户名或密码输入错误！");
		return "devLogin";
	}
	

	@RequestMapping(value="/categoryLevel2",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getAllcategoryLevel2(Integer categoryLevel1){
		List<AppCategory> list=appCategoryBiz.getAppCategoryByParendId(categoryLevel1);
		String result=JSON.toJSONString(list,true);
		return result;
	}
	
	
	@RequestMapping(value="/categoryLevel3",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getAllcategoryLevel3(Integer categoryLevel2){
		List<AppCategory> list=appCategoryBiz.getAppCategoryByParendId(categoryLevel2);
		String result=JSON.toJSONString(list,true);
		return result;
	}
	@RequestMapping("/updateApp")
	public String updateApp(Model model,Integer id,AppInfo appInfo){
		System.out.println("jinglsil");
		
		model.addAttribute("app", appInfoBiz.getById(id));
		List<AppVersion> list = appVersionBiz.getByAppId(id);
		model.addAttribute("categoryLevel1List", appCategoryBiz.getAppCategoryByParendId(0));
		model.addAttribute("appFlatformList", dataDictionaryBiz.getDictionary("APP_FLATFORM"));
		model.addAttribute("appVersions", list);

	
		
		return "updateApp";
	}
	@RequestMapping(value="doupdateApp",method=RequestMethod.POST)
	public String updateApps(Model model,AppInfo appInfo){		
		System.out.println(appInfo.getSoftwareName()+"djkjdk");
		
		appVersionBiz.updates(appInfo);
		return "updateApp";
	}
	
	
	@RequestMapping("/selectAppInfo")
	public String selectAppInfo(Model model,Integer id){
		model.addAttribute("app", appInfoBiz.getById(id));
		List<AppVersion> list = appVersionBiz.getByAppId(id);
		if(list.size() == 0){
			list = null;
		}
		model.addAttribute("appVersions", list);
		return "selectAppInfo";
	}
	
	@RequestMapping("/deleteAppInfo")
	public void deleteAppInfo(Model model,Integer id,HttpServletResponse response){
		appVersionBiz.deleteVersion(id);
		int count = appInfoBiz.deleteAppInfo(id);
		try {
			PrintWriter out = response.getWriter();
			out.print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/updateStatus")
	public void updateStatus(Model model,Integer id,Integer typeNumber,HttpServletResponse response){
		int count = appInfoBiz.updateOnOff(id, typeNumber, new Date());
		try {
			PrintWriter out = response.getWriter();
			out.print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/updateVersion")
	public String updateVersion(Model model,Integer appId){
		model.addAttribute("newVersion", appInfoBiz.getById(appId).getVersion());
		model.addAttribute("appVersions", appVersionBiz.getByAppId(appId));
		return "updateVersion";
	}
	
	@RequestMapping("/reUpdateVersion")
	public String reUpdateVersion(Model model,AppVersion appVersion,HttpSession session){
		appVersion.setModifyBy((DevUser)session.getAttribute("user"));
		appVersion.setModifyDate(new Date());
		appVersionBiz.updateVersion(appVersion);
		return "redirect:appmaintain";
	}
}
